/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-12-13 14:15:07
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */

const Service = require("egg").Service;
const timeFormart = require("../extends/timeFormart");
class HousingService extends Service {
    //获取列表
    async getHousingList() {
            const { ctx, app } = this;
            const { province, city, area, keycode, is_lease } = ctx.query;
            let res = await app.mysql.select("housing");
            res = res.filter((item) => item.is_lease * 1 === is_lease * 1);
            let total = res.length;
            if (province) {
                res = res.filter((item) => item.province.includes(province));
                total = res.length;
            }
            if (city && city !== province + "市") {
                res = res.filter((item) => item.city.includes(city));
                total = res.length;
            }
            if (area) {
                res = res.filter((item) => item.area.includes(area));
                total = res.length;
            }
            if (keycode) {
                res = res.filter((item) => item.name.includes(keycode));
                total = res.length;
            }
            res.sort((a, b) => {
                return b.order - a.order;
            });
            return {
                total,
                res,
            };
        }
        //添加
    async addHousingList() {
            const { ctx, app } = this;
            const { name, province, city, area, price, order, state, broker, is_lease } = ctx.request.body;
            const res = await app.mysql.insert("housing", {
                ...ctx.request.body,
                update_time: timeFormart(Date.now()),
            });
            return res;
        }
        //删除
    async delHousingList() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.delete("housing", { id });
            return res;
        }
        //修改
    async editHousingList() {
            const { ctx, app } = this;
            const { name, province, city, area, price, order, state, broker, id } = ctx.request.body;
            const res = await app.mysql.update("housing", {
                id,
                ...ctx.request.body,
                update_time: timeFormart(Date.now()),
            });
            return res;
        }
        //改变状态
    async changeHousingState() {
            const { ctx, app } = this;
            const { id } = ctx.query;
            const res = await app.mysql.get("housing", { id });
            const state = res.state * 1;
            const result = await app.mysql.update("housing", {
                id,
                state: Number(!state),
            });
            return result;
        }
        //指派经纪人
    async designBroker() {
        const { ctx, app } = this;
        const { id, broker } = ctx.request.body;
        const res = await app.mysql.update("housing", { id, broker });
        return res;
    }
}
module.exports = HousingService;